class Solution
{
public:
    int minOperations(vector<int> &nums, int x)
    {
        int tem=-1;
        int sum=0;int total=0;
        for(auto&ch:nums)
        {
            total+=ch;
        }
        if(total-x<0) return -1;
        for (int left = 0, right = 0; right < nums.size();)
        {
            sum+=nums[right++];
            //出窗口
            while(sum>total-x)
            {
                sum-=nums[left++];
            }
            if(sum==total-x)
            {
                //此时跟新结果
                tem=max(tem,right-left);
            }
        }
        if(tem==-1) return -1;
        return nums.size()-tem;
    }
};